package threadLearn;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.google.common.collect.Sets;

public class ThreadProblem2 implements Runnable{

	static ThreadProblem2 instance = new ThreadProblem2();
	
	//static Set<Integer> count = new HashSet<>();
	//static Set<Integer> count = Collections.newSetFromMap(new HashMap<>());
	static Set<Integer> count = Sets.newConcurrentHashSet();
	Lock lock = new ReentrantLock();
	
	@Override
	public void run() {
		
		method();
	}
	
	private void method() {
		
		//lock.lock();
		System.out.println(Thread.currentThread()+"：开始了");
		for(int i = 0; i < 100000; i++) {
			count.add(i);
		}
		//lock.unlock();
		System.out.println(Thread.currentThread()+"：结束了");
	}

	public static void main(String[] args) {
		Thread t1 = new Thread(instance);
		Thread t2 = new Thread(instance);
		t1.start();
		t2.start();
		while(t1.isAlive() || t2.isAlive()) {
			//只要t1或t2没结束，就不执行main进程
		}
		System.out.println(count.size());
		System.out.println("finised...");
		
	}

}

